/*
 * @Date: 2025-06-13 10:50:10
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-08-29 17:59:13
 * @FilePath: \corpus\src\components\Aksk\index.tsx
 */
import { deleteKey, generate, getAksk, updateApp } from '@/services/ant-design-pro/foundation';
import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components';
import { Button, Form, Input, Modal, Popconfirm } from 'antd';
import type React from 'react';
import { useRef } from 'react';
const Aksk: React.FC = (props: any) => {
  const { akskVisible, handleCancel } = props;

  const title = '生成Aksk';
  const actionRef = useRef<ActionType>();
  const columns: ProColumns<any>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
      fixed: 'left',
      title: '序号',
    },

    {
      title: 'AKSK名称',
      dataIndex: 'appName',
      ellipsis: true,
      fixed: 'left',
      width: 126,
    },

    {
      title: 'AK',
      dataIndex: 'appKey',
      width: 200,
      ellipsis: true,
      hideInSearch: true,
    },

    {
      title: 'SK',
      dataIndex: 'appSecret',
      width: 200,
      ellipsis: true,
      hideInSearch: true,
    },

    {
      title: 'AKSK更新时间',
      dataIndex: 'createTime',
      ellipsis: true,
      width: 146,
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      ellipsis: true,
      hideInSearch: true,
      render: (_, record) => {
        return record.status === 1 ? '启用' : '禁用';
      },
    },
    {
      title: '操作',
      width: 100,
      dataIndex: 'option',
      hideInSearch: true,

      fixed: 'right',

      render: (_, record: any) => [
        <Popconfirm
          key={1}
          title="修改状态"
          description={`确认修改当前状态？`}
          onConfirm={() => {
            updateApp({ status: record.status === 1 ? 0 : 1, id: record.id }).then((res: any) => {
              if (res?.success) {
                actionRef.current?.reload();
              }
            });
          }}
          onCancel={() => {}}
          okText="确认"
          cancelText="取消"
        >
          <a style={{ marginRight: '10.0px' }}>{record.status === 1 ? '禁用' : '启用'}</a>
        </Popconfirm>,

        <Popconfirm
          key={1}
          title="删除AKSK"
          description={`确认删除当前AKSK？`}
          onConfirm={() => {
            deleteKey(record.id).then((res: any) => {
              if (res?.success) {
                actionRef.current?.reload();
              }
            });
          }}
          onCancel={() => {}}
          okText="确认"
          cancelText="取消"
        >
          <a style={{ marginRight: '10.0px' }}>删除</a>
        </Popconfirm>,
      ],
    },
  ];
  const [form] = Form.useForm();

  const handleSubmit = () => {
    form.validateFields().then(async (vals) => {
      console.log(vals);
      const res = await generate({ appName: vals.appName });
      console.log(res);
      if (res.success) {
        actionRef.current?.reload();
        form.resetFields();
      }
    });
  };
  return (
    <Modal
      width={1000}
      title={title}
      open={akskVisible}
      onCancel={() => {
        handleCancel?.();
      }}
      onOk={handleSubmit}
    >
      <div style={{ padding: '10.0px' }}>
        <Form style={{ marginBottom: '20.0px' }} layout={'inline'} form={form}>
          {/* <Form.Item label="AK">
            <Input disabled placeholder="请输入" />
          </Form.Item>
          <Form.Item label="SK">
            <Input disabled placeholder="请输入" />
          </Form.Item> */}
          <Form.Item
            label="AKSK名称"
            name="appName"
            rules={[{ required: true, message: '请选择' }]}
          >
            <Input placeholder="请输入" />
          </Form.Item>
          <Form.Item>
            <Popconfirm
              key={1}
              title="生成AKSK"
              description={`确认生成AKSK？`}
              onConfirm={() => {
                handleSubmit();
              }}
              onCancel={() => {}}
              okText="确认"
              cancelText="取消"
            >
              <Button type="primary">点击生成</Button>
            </Popconfirm>
          </Form.Item>
        </Form>
        <ProTable<API.RuleListItem, API.PageParams>
          toolBarRender={() => []}
          rowKey="key"
          cardBordered
          actionRef={actionRef}
          request={async (
            // 第一个参数 params 查询表单和 params 参数的结合
            // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
            params: any,
          ) => {
            // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
            // 如果需要转化参数可以在这里进行修改

            const msg = await getAksk({
              ...params,
            });
            return {
              data: msg.data,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: true,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: msg.total,
            };
          }}
          search={{
            labelWidth: 'auto',
          }}
          pagination={{
            showSizeChanger: true,
            defaultPageSize: 10,
          }}
          columns={columns}
        />
      </div>
    </Modal>
  );
};

export default Aksk;
